<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: High Dynamic Range Imaging</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d3/dd5/tutorial_table_of_content_other.html">Other tutorials (ml, objdetect, photo, stitching, video)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">High Dynamic Range Imaging </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Next Tutorial:</b> <a class="el" href="../../d8/d19/tutorial_stitcher.html">High level stitching API (Stitcher class)</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Fedor Morozov </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<h2>Introduction </h2>
<p>Today most digital images and imaging devices use 8 bits per channel thus limiting the dynamic range of the device to two orders of magnitude (actually 256 levels), while human eye can adapt to lighting conditions varying by ten orders of magnitude. When we take photographs of a real world scene bright regions may be overexposed, while the dark ones may be underexposed, so we can’t capture all details using a single exposure. HDR imaging works with images that use more that 8 bits per channel (usually 32-bit float values), allowing much wider dynamic range.</p>
<p>There are different ways to obtain HDR images, but the most common one is to use photographs of the scene taken with different exposure values. To combine this exposures it is useful to know your camera’s response function and there are algorithms to estimate it. After the HDR image has been blended it has to be converted back to 8-bit to view it on usual displays. This process is called tonemapping. Additional complexities arise when objects of the scene or camera move between shots, since images with different exposures should be registered and aligned.</p>
<p>In this tutorial we show how to generate and display HDR image from an exposure sequence. In our case images are already aligned and there are no moving objects. We also demonstrate an alternative approach called exposure fusion that produces low dynamic range image. Each step of HDR pipeline can be implemented using different algorithms so take a look at the reference manual to see them all.</p>
<h2>Exposure sequence </h2>
<div class="image">
<img src="../../memorial.png" alt="memorial.png"/>
</div>
<h2>Source Code </h2>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><p> This tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/photo/hdr_imaging/hdr_imaging.cpp">here</a> </p><div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../dc/d81/photo_8hpp.html">opencv2/photo.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;vector&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;fstream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> loadExposureSeq(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>, vector&lt;Mat&gt;&amp;, vector&lt;float&gt;&amp;);</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>**argv)</div><div class="line">{</div><div class="line">    <a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a> parser( argc, argv, <span class="stringliteral">&quot;{@input | | Input directory that contains images and exposure times. }&quot;</span> );</div><div class="line"></div><div class="line">    vector&lt;Mat&gt; images;</div><div class="line">    vector&lt;float&gt; times;</div><div class="line">    loadExposureSeq(parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;( <span class="stringliteral">&quot;@input&quot;</span> ), images, times);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> response;</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt;CalibrateDebevec&gt;</a> <a class="code" href="../../db/d58/group__calib3d__fisheye.html#gad626a78de2b1dae7489e152a5a5a89e1">calibrate</a> = <a class="code" href="../../d6/df5/group__photo__hdr.html#ga7fed9707ad5f2cc0e633888867109f90">createCalibrateDebevec</a>();</div><div class="line">    calibrate-&gt;process(images, response, times);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> hdr;</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt;MergeDebevec&gt;</a> merge_debevec = <a class="code" href="../../d6/df5/group__photo__hdr.html#gaa8eab36bc764abb2a225db7c945f87f9">createMergeDebevec</a>();</div><div class="line">    merge_debevec-&gt;process(images, hdr, times, response);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> ldr;</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt;Tonemap&gt;</a> tonemap = <a class="code" href="../../d6/df5/group__photo__hdr.html#gabcbd653140b93a1fa87ccce94548cd0d">createTonemap</a>(2.2f);</div><div class="line">    tonemap-&gt;process(hdr, ldr);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> fusion;</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt;MergeMertens&gt;</a> merge_mertens = <a class="code" href="../../d6/df5/group__photo__hdr.html#ga79d59aa3cb3a7c664e59a4b5acc1ccb6">createMergeMertens</a>();</div><div class="line">    merge_mertens-&gt;process(images, fusion);</div><div class="line"></div><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;fusion.png&quot;</span>, fusion * 255);</div><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;ldr.png&quot;</span>, ldr * 255);</div><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;hdr.hdr&quot;</span>, hdr);</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> loadExposureSeq(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> path, vector&lt;Mat&gt;&amp; images, vector&lt;float&gt;&amp; times)</div><div class="line">{</div><div class="line">    path = path + <span class="stringliteral">&quot;/&quot;</span>;</div><div class="line">    ifstream list_file((path + <span class="stringliteral">&quot;list.txt&quot;</span>).c_str());</div><div class="line">    <span class="keywordtype">string</span> name;</div><div class="line">    <span class="keywordtype">float</span> val;</div><div class="line">    <span class="keywordflow">while</span>(list_file &gt;&gt; name &gt;&gt; val) {</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(path + name);</div><div class="line">        images.push_back(img);</div><div class="line">        times.push_back(1 / val);</div><div class="line">    }</div><div class="line">    list_file.close();</div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><p> This tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/photo/hdr_imaging/HDRImagingDemo.java">here</a> </p><div class="fragment"><div class="line"><span class="keyword">import</span> java.io.IOException;</div><div class="line"><span class="keyword">import</span> java.nio.file.Files;</div><div class="line"><span class="keyword">import</span> java.nio.file.Paths;</div><div class="line"><span class="keyword">import</span> java.util.ArrayList;</div><div class="line"><span class="keyword">import</span> java.util.List;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.opencv.core.Core;</div><div class="line"><span class="keyword">import</span> org.opencv.core.CvType;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Mat;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Scalar;</div><div class="line"><span class="keyword">import</span> org.opencv.imgcodecs.Imgcodecs;</div><div class="line"><span class="keyword">import</span> org.opencv.photo.CalibrateDebevec;</div><div class="line"><span class="keyword">import</span> org.opencv.photo.MergeDebevec;</div><div class="line"><span class="keyword">import</span> org.opencv.photo.MergeMertens;</div><div class="line"><span class="keyword">import</span> org.opencv.photo.Photo;</div><div class="line"><span class="keyword">import</span> org.opencv.photo.Tonemap;</div><div class="line"></div><div class="line"><span class="keyword">class </span>HDRImaging {</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> loadExposureSeq(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> path, List&lt;Mat&gt; images, List&lt;Float&gt; times) {</div><div class="line">        path += <span class="stringliteral">&quot;/&quot;</span>;</div><div class="line"></div><div class="line">        List&lt;String&gt; lines;</div><div class="line">        <span class="keywordflow">try</span> {</div><div class="line">            lines = Files.readAllLines(<a class="code" href="../../d3/dc4/namespacecv_1_1utils.html#a8a925b06bb0e09dede483e22333ad4dd">Paths</a>.get(path + <span class="stringliteral">&quot;list.txt&quot;</span>));</div><div class="line"></div><div class="line">            <span class="keywordflow">for</span> (<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> line : lines) {</div><div class="line">                <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] splitStr = line.split(<span class="stringliteral">&quot;\\s+&quot;</span>);</div><div class="line">                <span class="keywordflow">if</span> (splitStr.length == 2) {</div><div class="line">                    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> name = splitStr[0];</div><div class="line">                    Mat img = Imgcodecs.imread(path + name);</div><div class="line">                    images.add(img);</div><div class="line">                    <span class="keywordtype">float</span> val = Float.parseFloat(splitStr[1]);</div><div class="line">                    times.add(1/ val);</div><div class="line">                }</div><div class="line">            }</div><div class="line">        } <span class="keywordflow">catch</span> (IOException e) {</div><div class="line">            e.printStackTrace();</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> path = args.length &gt; 0 ? args[0] : <span class="stringliteral">&quot;&quot;</span>;</div><div class="line">        <span class="keywordflow">if</span> (path.isEmpty()) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Path is empty. Use the directory that contains images and exposure times.&quot;</span>);</div><div class="line">            System.exit(0);</div><div class="line">        }</div><div class="line"></div><div class="line">        List&lt;Mat&gt; images = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">        List&lt;Float&gt; times = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">        loadExposureSeq(path, images, times);</div><div class="line"></div><div class="line">        Mat response = <span class="keyword">new</span> Mat();</div><div class="line">        CalibrateDebevec calibrate = Photo.createCalibrateDebevec();</div><div class="line">        Mat matTimes = <span class="keyword">new</span> Mat(times.size(), 1, CvType.CV_32F);</div><div class="line">        <span class="keywordtype">float</span>[] arrayTimes = <span class="keyword">new</span> <span class="keywordtype">float</span>[(int) (matTimes.total()*matTimes.channels())];</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; times.size(); i++) {</div><div class="line">            arrayTimes[i] = times.get(i);</div><div class="line">        }</div><div class="line">        matTimes.put(0, 0, arrayTimes);</div><div class="line">        calibrate.process(images, response, matTimes);</div><div class="line"></div><div class="line">        Mat hdr = <span class="keyword">new</span> Mat();</div><div class="line">        MergeDebevec mergeDebevec = Photo.createMergeDebevec();</div><div class="line">        mergeDebevec.process(images, hdr, matTimes);</div><div class="line"></div><div class="line">        Mat ldr = <span class="keyword">new</span> Mat();</div><div class="line">        Tonemap tonemap = Photo.createTonemap(2.2f);</div><div class="line">        tonemap.process(hdr, ldr);</div><div class="line"></div><div class="line">        Mat fusion = <span class="keyword">new</span> Mat();</div><div class="line">        MergeMertens mergeMertens = Photo.createMergeMertens();</div><div class="line">        mergeMertens.process(images, fusion);</div><div class="line"></div><div class="line">        Core.multiply(fusion, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,255,255), fusion);</div><div class="line">        Core.multiply(ldr, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,255,255), ldr);</div><div class="line">        Imgcodecs.imwrite(<span class="stringliteral">&quot;fusion.png&quot;</span>, fusion);</div><div class="line">        Imgcodecs.imwrite(<span class="stringliteral">&quot;ldr.png&quot;</span>, ldr);</div><div class="line">        Imgcodecs.imwrite(<span class="stringliteral">&quot;hdr.hdr&quot;</span>, hdr);</div><div class="line"></div><div class="line">        System.exit(0);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>HDRImagingDemo {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="comment">// Load the native OpenCV library</span></div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line"></div><div class="line">        <span class="keyword">new</span> HDRImaging().run(args);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><p> This tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/photo/hdr_imaging/hdr_imaging.py">here</a> </p><div class="fragment"><div class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> print_function</div><div class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> division</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"><span class="keyword">import</span> argparse</div><div class="line"><span class="keyword">import</span> os</div><div class="line"></div><div class="line"><span class="keyword">def </span>loadExposureSeq(path):</div><div class="line">    images = []</div><div class="line">    times = []</div><div class="line">    with <a class="code" href="../../d6/dee/group__hdf5.html#ga243d7e303690af3c5c3686ca5785205e">open</a>(os.path.join(path, <span class="stringliteral">&#39;list.txt&#39;</span>)) <span class="keyword">as</span> f:</div><div class="line">        content = f.readlines()</div><div class="line">    <span class="keywordflow">for</span> line <span class="keywordflow">in</span> content:</div><div class="line">        tokens = line.split()</div><div class="line">        images.append(<a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(os.path.join(path, tokens[0])))</div><div class="line">        times.append(1 / float(tokens[1]))</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> images, np.asarray(times, dtype=np.float32)</div><div class="line"></div><div class="line">parser = argparse.ArgumentParser(description=<span class="stringliteral">&#39;Code for High Dynamic Range Imaging tutorial.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;--input&#39;</span>, type=str, help=<span class="stringliteral">&#39;Path to the directory that contains images and exposure times.&#39;</span>)</div><div class="line">args = parser.parse_args()</div><div class="line"></div><div class="line"><span class="keywordflow">if</span> <span class="keywordflow">not</span> args.input:</div><div class="line">    parser.print_help()</div><div class="line">    exit(0)</div><div class="line"></div><div class="line"></div><div class="line">images, times = loadExposureSeq(args.input)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">calibrate = <a class="code" href="../../d6/df5/group__photo__hdr.html#ga7fed9707ad5f2cc0e633888867109f90">cv.createCalibrateDebevec</a>()</div><div class="line">response = calibrate.process(images, times)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">merge_debevec = <a class="code" href="../../d6/df5/group__photo__hdr.html#gaa8eab36bc764abb2a225db7c945f87f9">cv.createMergeDebevec</a>()</div><div class="line">hdr = merge_debevec.process(images, times, response)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">tonemap = <a class="code" href="../../d6/df5/group__photo__hdr.html#gabcbd653140b93a1fa87ccce94548cd0d">cv.createTonemap</a>(2.2)</div><div class="line">ldr = tonemap.process(hdr)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">merge_mertens = <a class="code" href="../../d6/df5/group__photo__hdr.html#ga79d59aa3cb3a7c664e59a4b5acc1ccb6">cv.createMergeMertens</a>()</div><div class="line">fusion = merge_mertens.process(images)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(<span class="stringliteral">&#39;fusion.png&#39;</span>, fusion * 255)</div><div class="line"><a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(<span class="stringliteral">&#39;ldr.png&#39;</span>, ldr * 255)</div><div class="line"><a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(<span class="stringliteral">&#39;hdr.hdr&#39;</span>, hdr)</div><div class="line"></div></div><!-- fragment -->  </div> <h2>Sample images </h2>
<p>Data directory that contains images, exposure times and <code>list.txt</code> file can be downloaded from <a href="https://github.com/opencv/opencv_extra/tree/master/testdata/cv/hdr/exposures">here</a>.</p>
<h2>Explanation </h2>
<ul>
<li><b>Load images and exposure times</b></li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    vector&lt;Mat&gt; images;</div><div class="line">    vector&lt;float&gt; times;</div><div class="line">    loadExposureSeq(parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;( <span class="stringliteral">&quot;@input&quot;</span> ), images, times);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        List&lt;Mat&gt; images = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">        List&lt;Float&gt; times = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">        loadExposureSeq(path, images, times);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">images, times = loadExposureSeq(args.input)</div></div><!-- fragment --> </div> <p>Firstly we load input images and exposure times from user-defined folder. The folder should contain images and <em>list.txt</em> - file that contains file names and inverse exposure times.</p>
<p>For our image sequence the list is following: </p><div class="fragment"><div class="line">memorial00.png 0.03125</div><div class="line">memorial01.png 0.0625</div><div class="line">...</div><div class="line">memorial15.png 1024</div></div><!-- fragment --><ul>
<li><b>Estimate camera response</b></li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat response;</div><div class="line">    Ptr&lt;CalibrateDebevec&gt; calibrate = <a class="code" href="../../d6/df5/group__photo__hdr.html#ga7fed9707ad5f2cc0e633888867109f90">createCalibrateDebevec</a>();</div><div class="line">    calibrate-&gt;process(images, response, times);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Mat response = <span class="keyword">new</span> Mat();</div><div class="line">        CalibrateDebevec calibrate = Photo.createCalibrateDebevec();</div><div class="line">        Mat matTimes = <span class="keyword">new</span> Mat(times.size(), 1, CvType.CV_32F);</div><div class="line">        <span class="keywordtype">float</span>[] arrayTimes = <span class="keyword">new</span> <span class="keywordtype">float</span>[(int) (matTimes.total()*matTimes.channels())];</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; times.size(); i++) {</div><div class="line">            arrayTimes[i] = times.get(i);</div><div class="line">        }</div><div class="line">        matTimes.put(0, 0, arrayTimes);</div><div class="line">        calibrate.process(images, response, matTimes);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">calibrate = <a class="code" href="../../d6/df5/group__photo__hdr.html#ga7fed9707ad5f2cc0e633888867109f90">cv.createCalibrateDebevec</a>()</div><div class="line">response = calibrate.process(images, times)</div></div><!-- fragment --> </div> <p>It is necessary to know camera response function (CRF) for a lot of HDR construction algorithms. We use one of the calibration algorithms to estimate inverse CRF for all 256 pixel values.</p>
<ul>
<li><b>Make HDR image</b></li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat hdr;</div><div class="line">    Ptr&lt;MergeDebevec&gt; merge_debevec = <a class="code" href="../../d6/df5/group__photo__hdr.html#gaa8eab36bc764abb2a225db7c945f87f9">createMergeDebevec</a>();</div><div class="line">    merge_debevec-&gt;process(images, hdr, times, response);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Mat hdr = <span class="keyword">new</span> Mat();</div><div class="line">        MergeDebevec mergeDebevec = Photo.createMergeDebevec();</div><div class="line">        mergeDebevec.process(images, hdr, matTimes);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">merge_debevec = <a class="code" href="../../d6/df5/group__photo__hdr.html#gaa8eab36bc764abb2a225db7c945f87f9">cv.createMergeDebevec</a>()</div><div class="line">hdr = merge_debevec.process(images, times, response)</div></div><!-- fragment --> </div> <p>We use Debevec's weighting scheme to construct HDR image using response calculated in the previous item.</p>
<ul>
<li><b>Tonemap HDR image</b></li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat ldr;</div><div class="line">    Ptr&lt;Tonemap&gt; tonemap = <a class="code" href="../../d6/df5/group__photo__hdr.html#gabcbd653140b93a1fa87ccce94548cd0d">createTonemap</a>(2.2f);</div><div class="line">    tonemap-&gt;process(hdr, ldr);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Mat ldr = <span class="keyword">new</span> Mat();</div><div class="line">        Tonemap tonemap = Photo.createTonemap(2.2f);</div><div class="line">        tonemap.process(hdr, ldr);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">tonemap = <a class="code" href="../../d6/df5/group__photo__hdr.html#gabcbd653140b93a1fa87ccce94548cd0d">cv.createTonemap</a>(2.2)</div><div class="line">ldr = tonemap.process(hdr)</div></div><!-- fragment --> </div> <p>Since we want to see our results on common LDR display we have to map our HDR image to 8-bit range preserving most details. It is the main goal of tonemapping methods. We use tonemapper with bilateral filtering and set 2.2 as the value for gamma correction.</p>
<ul>
<li><b>Perform exposure fusion</b></li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat fusion;</div><div class="line">    Ptr&lt;MergeMertens&gt; merge_mertens = <a class="code" href="../../d6/df5/group__photo__hdr.html#ga79d59aa3cb3a7c664e59a4b5acc1ccb6">createMergeMertens</a>();</div><div class="line">    merge_mertens-&gt;process(images, fusion);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Mat fusion = <span class="keyword">new</span> Mat();</div><div class="line">        MergeMertens mergeMertens = Photo.createMergeMertens();</div><div class="line">        mergeMertens.process(images, fusion);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">merge_mertens = <a class="code" href="../../d6/df5/group__photo__hdr.html#ga79d59aa3cb3a7c664e59a4b5acc1ccb6">cv.createMergeMertens</a>()</div><div class="line">fusion = merge_mertens.process(images)</div></div><!-- fragment --> </div> <p>There is an alternative way to merge our exposures in case when we don't need HDR image. This process is called exposure fusion and produces LDR image that doesn't require gamma correction. It also doesn't use exposure values of the photographs.</p>
<ul>
<li><b>Write results</b></li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;fusion.png&quot;</span>, fusion * 255);</div><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;ldr.png&quot;</span>, ldr * 255);</div><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;hdr.hdr&quot;</span>, hdr);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Core.multiply(fusion, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,255,255), fusion);</div><div class="line">        Core.multiply(ldr, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,255,255), ldr);</div><div class="line">        Imgcodecs.imwrite(<span class="stringliteral">&quot;fusion.png&quot;</span>, fusion);</div><div class="line">        Imgcodecs.imwrite(<span class="stringliteral">&quot;ldr.png&quot;</span>, ldr);</div><div class="line">        Imgcodecs.imwrite(<span class="stringliteral">&quot;hdr.hdr&quot;</span>, hdr);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(<span class="stringliteral">&#39;fusion.png&#39;</span>, fusion * 255)</div><div class="line"><a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(<span class="stringliteral">&#39;ldr.png&#39;</span>, ldr * 255)</div><div class="line"><a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(<span class="stringliteral">&#39;hdr.hdr&#39;</span>, hdr)</div></div><!-- fragment --> </div> <p>Now it's time to look at the results. Note that HDR image can't be stored in one of common image formats, so we save it to Radiance image (.hdr). Also all HDR imaging functions return results in [0, 1] range so we should multiply result by 255.</p>
<p>You can try other tonemap algorithms: <a class="el" href="../../da/d53/classcv_1_1TonemapDrago.html" title="Adaptive logarithmic mapping is a fast global tonemapping algorithm that scales the image in logarith...">cv::TonemapDrago</a>, <a class="el" href="../../de/d76/classcv_1_1TonemapMantiuk.html" title="This algorithm transforms image to contrast using gradients on all levels of gaussian pyramid...">cv::TonemapMantiuk</a> and <a class="el" href="../../d0/dec/classcv_1_1TonemapReinhard.html" title="This is a global tonemapping operator that models human visual system. ">cv::TonemapReinhard</a> You can also adjust the parameters in the HDR calibration and tonemap methods for your own photos.</p>
<h2>Results </h2>
<h3>Tonemapped image</h3>
<div class="image">
<img src="../../ldr.png" alt="ldr.png"/>
</div>
<h3>Exposure fusion</h3>
<div class="image">
<img src="../../fusion.png" alt="fusion.png"/>
</div>
<h2>Additional Resources </h2>
<ol type="1">
<li>Paul E Debevec and Jitendra Malik. Recovering high dynamic range radiance maps from photographs. In ACM SIGGRAPH 2008 classes, page 31. ACM, 2008. <a class="el" href="../../d0/de3/citelist.html#CITEREF_DM97">[55]</a></li>
<li>Mark A Robertson, Sean Borman, and Robert L Stevenson. Dynamic range improvement through multiple exposures. In Image Processing, 1999. ICIP 99. Proceedings. 1999 International Conference on, volume 3, pages 159–163. IEEE, 1999. <a class="el" href="../../d0/de3/citelist.html#CITEREF_RB99">[205]</a></li>
<li>Tom Mertens, Jan Kautz, and Frank Van Reeth. Exposure fusion. In Computer Graphics and Applications, 2007. PG'07. 15th Pacific Conference on, pages 382–390. IEEE, 2007. <a class="el" href="../../d0/de3/citelist.html#CITEREF_MK07">[168]</a></li>
<li><a href="https://en.wikipedia.org/wiki/High-dynamic-range_imaging">Wikipedia-HDR</a></li>
<li><a href="http://www.pauldebevec.com/Research/HDR/">Recovering High Dynamic Range Radiance Maps from Photographs (webpage)</a> </li>
</ol>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:37 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
